﻿using System;
using System.Linq;
using System.Reflection;
using System.Xml;
using SQLPXBase.Factory.Keys;

namespace SQLPXBase.Factory.AutoLogging
{
    public interface ILoggableModel
    {
        void SaveLog(bool isInsert = false);
    }

    //public abstract class BaseLoggableModel<PX> : ChangeAwareness.ChangeAwareBaseModel<PX>
    //    where PX : SQLProxyBase, new()
    //{
    //    public BaseLoggableModel(ModelFactory<PX> factory) : base(factory)
    //    {
    //    }

    //    public abstract void SaveLog(bool isInsert = false);

    //    public virtual void WriteXml(XmlWriter w)
    //    {
    //        this._WriteXml(w);

    //        //w.WriteStartElement(this.GetType().ToString());
    //        //w.WriteAttributeString("ID", GetPK().ToString());

    //        //Type t = this.GetType();
    //        //PropertyInfo[] p = t.GetProperties(BindingFlags.Public | BindingFlags.Instance);
    //        //foreach (
    //        //    PropertyInfo info in
    //        //        p.Where(
    //        //            property =>
    //        //                property.CanRead && property.Name != "factory" &&
    //        //                (property.GetCustomAttributes(typeof(Attributes.DoNotLogAttribute), false).Length == 0)))
    //        //    w.WriteElementString(
    //        //        info.Name,
    //        //        //(info.GetValue(this, null) ?? "").ToString()
    //        //        info.GetValue(this, null) is BaseLazyKey<PX>
    //        //            ? ((BaseLazyKey<PX>)info.GetValue(this, null)).Id.ToString()
    //        //            : ((info.GetValue(this, null) ?? "").ToString())
    //        //        );
    //        //w.WriteEndElement();
    //    }
    //}

    public static class LoggableModel
    {
        public static void WriteXml<PX>(this FactorizableModel<PX> model, XmlWriter w) where PX : Interfaces.ISQLProxyBase, new()
        {
            w.WriteStartElement(model.GetType().ToString());
            w.WriteAttributeString("ID", model.GetPK().ToString());

            Type t = model.GetType();
            PropertyInfo[] p = t.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (
                PropertyInfo info in
                    p.Where(
                        property =>
                            property.CanRead && property.Name != "factory" &&
                            (property.GetCustomAttributes(typeof(Attributes.DoNotLogAttribute), false).Length == 0)))
                w.WriteElementString(
                    info.Name,
                    //(info.GetValue(this, null) ?? "").ToString()
                    info.GetValue(model, null) is BaseLazyKey<PX>
                        ? ((BaseLazyKey<PX>)info.GetValue(model, null)).Id.ToString()
                        : ((info.GetValue(model, null) ?? "").ToString())
                    );
            w.WriteEndElement();
        }
    }
}
